﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class AchievementList : MonoBehaviour
{
    public static AchievementList instance;
    // 업적셀 프리펩
    static GameObject s_Prefab;
    // 업적셀 프리펩 리소스 위치
    const string PrefabPath = "Prefabs/Achievement/AchievementList";
    // 업적셀 갯수
    static AchievementListCell[] s_AchievementCellList;

    bool isCanClose = false;

    public static void CreateAchievementList()
    {
        if (instance == null) // 재사용 가능
        {
            if (s_Prefab == null)
                s_Prefab = Resources.Load(PrefabPath) as GameObject;

            GameObject tempObj = Instantiate(s_Prefab) as GameObject;
            
            GameObject UiRoot = GameObject.Find("UI Root (2D)/Camera");

            tempObj.transform.parent = UiRoot.transform;
            tempObj.transform.localScale = Vector3.one;
            tempObj.transform.localPosition = Vector3.zero;

            instance = tempObj.GetComponent("AchievementList") as AchievementList;            
        }

        instance.CreateAchievementListCells();
        instance.gameObject.SetActive(true);

        instance.transform.localScale = Vector3.one / 10;
        iTween.ScaleTo(instance.gameObject, iTween.Hash("easetype", "spring", "islocal", true, "time", 0.5f, "scale", Vector3.one, "oncompletetarget", instance.gameObject, "oncomplete", "Showcomplte"));
    }

    void CreateAchievementListCells()
    {
        IDictionary<int, CAchievementData>  AchievementDataDic = CAchievementDataManager.Instance.GetAchievementMap();
        
        ICollection<int> temp = AchievementDataDic.Keys;

        int[] SortedKeys = new int[temp.Count];
        temp.CopyTo(SortedKeys, 0);
        Array.Sort(SortedKeys);          

        int MaxOfAchieveMent = 0;  //중복 되지않는 업적 갯수
        LogCounterType preLogName = LogCounterType.NONE;
        foreach (int tempID in SortedKeys)
        {
            if (AchievementDataDic[tempID].log_name.CompareTo(preLogName) != 0)
            {
                MaxOfAchieveMent++;
                preLogName = AchievementDataDic[tempID].log_name;
            }
        }

        if (s_AchievementCellList == null)
            s_AchievementCellList = new AchievementListCell[MaxOfAchieveMent];

        Transform scrollPanelTransform = transform.FindChild("Panel");
        UIScrollView scrollView = scrollPanelTransform.GetComponent("UIScrollView") as UIScrollView;
        Transform GridTransform = scrollPanelTransform.FindChild("Grid");

        for (int i = 0; i < MaxOfAchieveMent; i++)
        {
            if (s_AchievementCellList[i] == null)
                s_AchievementCellList[i] = AchievementListCell.SetCell(i, GridTransform, scrollView);

            s_AchievementCellList[i].SetCellWithIndex(i);   // 재사용 가능 구조
        }
    }

    void Showcomplte()
    {
        isCanClose = true;
    }

    void OnOK()
    {
        if (isCanClose)
        {
            iTween.ScaleTo(gameObject, iTween.Hash("easetype", "spring", "islocal", true, "time", 0.5f, "scale", Vector3.zero, "oncompletetarget", gameObject, "oncomplete", "EndShow"));
        }
    }

    void EndShow()
    {
        isCanClose = false;
        gameObject.SetActive(false);    
    }
}
